package com.power.controller;

import com.power.domain.Basket;
import com.power.model.Result;
import com.power.service.BasketService;
import com.power.util.AuthUtils;
import com.power.vo.CartTotalAmount;
import com.power.vo.CartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 购物车业务控制层
 * @Author dingchunlin
 * @Date 2024/07/22 12:09
 * @Version 1.0.0
 */
@Api(tags = "购物车业务接口管理")
@RestController
@RequestMapping("/p/shopCart")
public class BasketController {

    @Autowired
    private BasketService basketService;

    /**
     * 查询会员购物车中商品的数量
     * @return Result<Integer>
     */
    @ApiOperation(value = "查询会员购物车中商品的数量")
    @GetMapping("/prodCount")
    public Result<Integer> loadMemberBasketProdCount() {
        String openId = AuthUtils.getMemberOpenId();
        Integer count = basketService.queryMemberBasketProdCount(openId);
        return Result.success(count);
    }

    /**
     * 查询会员购物车中商品列表
     * @return Result<CartVo>
     */
    @ApiOperation(value = "查询会员购物车中商品列表")
    @GetMapping("/info")
    public Result<CartVo> loadMemberCartVo() {
        CartVo cartVo = basketService.queryMemberCartVo();
        return Result.success(cartVo);
    }

    /**
     * 计算会员选中购物车中商品的总价
     * @param basketIds 购物车商品ID列表
     * @return  Result<CartTotalAmount>
     */
    @ApiOperation(value = "计算会员选中购物车中商品的总价")
    @PostMapping("/totalPay")
    public Result<CartTotalAmount> calculateMemberCheckedBasketTotalAmount(@RequestBody List<Long> basketIds) {
        CartTotalAmount cartTotalAmount = basketService.calculateMemberCheckedBasketTotalAmount(basketIds);
        return Result.success(cartTotalAmount);
    }

    /**
     * 修改购物车商品数量
     * @param basket 购物车商品对象
     * @return Result<String>
     */
    @ApiOperation(value = "修改购物车商品数量")
    @PostMapping("/changeItem")
    public Result<Boolean> changeMemberBasketItem(@RequestBody Basket basket) {
        Boolean result = basketService.changeMemberBasketItem(basket);
        return Result.success(result);
    }

    /**
     * 删除购物车商品
     * @param basketIds 购物车商品ID列表
     * @return Result<String>
     */
    @ApiOperation(value = "删除购物车商品")
    @DeleteMapping("/deleteItem")
    public Result<String> deleteMemberBasketItem(@RequestBody List<Long> basketIds) {
        Boolean result = basketService.removeBatchByIds(basketIds);
        return Result.handle(result);
    }

}